package ALBasicServer.ALProcess;

import java.util.ArrayList;
import java.util.List;

import ALBasicCommon.ALBasicCommonFun;
import ALServerLog.ALServerLog;

/************************
 * 多个步骤需要处理的时候通过本处理对象进行的过程控制处理
 * @author mj
 *
 */
public class ALStepProcess extends _AALProcess implements _IALProcessAction
{
    /// <summary>
    /// 创建一个多过程过程对象
    /// </summary>
    /// <returns></returns>
    public static ALStepProcess CreateStepProcess(String _processTag) { return new ALStepProcess(_processTag); }

    /// <summary>
    /// 需要执行的操作队列
    /// </summary>
    private List<_IALProcessDelegate> _m_lDealActionList;
    //步骤统计对象
    private ALStepCounter _m_scStepCounter;

    protected ALStepProcess(String _processTag)
    {
    	super(_processTag);
    	
        _m_lDealActionList = new ArrayList<_IALProcessDelegate>();
        
        _m_scStepCounter = new ALStepCounter();
    }
    
	/**************
	 * 单纯的一个纯函数处理
	 * 
	 * 这里直接处理完成
	 * @param _process
	 */
    @Override
	public void dealAction()
	{
    	_onProcessDone();
	}

    /// <summary>
    /// 添加一个执行过程
    /// </summary>
    /// <param name="_process"></param>
    /// <returns></returns>
    public ALStepProcess addProcess(_IALProcessDelegate _action)
    {
        if(null == _action)
            return this;

        if(isRunning())
        {
            ALServerLog.Error("add process when process started!");
            return this;
        }

        synchronized (_m_lDealActionList) {
	        //加入队列返回
	        _m_lDealActionList.add(_action);
        }

        return this;
    }

    /// <summary>
    /// 获取出来可以在监控对象外围补充输出的信息
    /// </summary>
    @Override
    protected String _processMonitorExInfo()
    {
        return "[Step: " + _m_scStepCounter.doneStep() + "/" + _m_scStepCounter.totalStep() + "]";
    }

    /// <summary>
    /// 进行处理
    /// </summary>
    @Override
    protected void _dealProcess(_IALProcessMonitor _monitor)
    {
        //无数据则直接完成
        if(_m_lDealActionList.size() <= 0)
        {
            _onProcessDone();
            return;
        }

        synchronized (_m_lDealActionList) {
	        //加入步骤
	        _m_scStepCounter.chgTotalStepCount(_m_lDealActionList.size());
	        _m_scStepCounter.setAllDoneDelegate(this);
	        //逐个直接开始
	        _IALProcessDelegate tmpAction = null;
	        for (int i = 0; i < _m_lDealActionList.size(); i++)
	        {
	            tmpAction = _m_lDealActionList.get(i);
	            //数据无效则直接完成一步
	            if (null == tmpAction)
	            {
	                _m_scStepCounter.addDoneStepCount();
	                continue;
	            }
	
	            //处理过程
	            try
	            {
	                tmpAction.dealAction(
	                		new _IALProcessAction() {
								@Override
								public void dealAction() {
									_m_scStepCounter.addDoneStepCount();
								}
							} );
	            }
	            catch (Exception _ex)
	            {
	                //调用回调
	                if (null != _monitor)
	                {
	                    _monitor.onErr(ALBasicCommonFun.getNowTimeMS() - processStartTimeMS(), getFullProcessTag(), _processMonitorExInfo(), _ex);
	                }
	                else
	                {
	                    //报错
	                    ALServerLog.Sys("Step Process " + getFullProcessTag() + " Fail! " + _processMonitorExInfo());
	                    ALServerLog.Error(_ex.getMessage());
	                }
	            }
	        }
	
	        _m_lDealActionList.clear();
        }
    }

    /// <summary>
    /// 当子处理对象完成时的处理
    /// </summary>
    @Override
    protected void _onChildDone()
    {
    }

    /// <summary>
    /// 重置处理
    /// </summary>
    @Override
    protected void _onDiscard()
    {
        synchronized (_m_lDealActionList) {
        	_m_lDealActionList.clear();
        }
        _m_scStepCounter.resetAll();
    }

    /// <summary>
    /// 完成的处理，需要放回缓存池
    /// </summary>
    @Override
    protected void _onDone()
    {
    }
}
